package com.sc.study.foundation.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.sc.study.clazz.domain.ScClazz;
import com.sc.study.clazz.domain.ScClazzSubject;
import com.sc.study.clazz.mapper.ScClazzMapper;
import com.sc.study.clazz.mapper.ScClazzSubjectMapper;
import com.sc.study.clazz.mapper.ScClazzSubjectTeacherMapper;
import com.sc.study.foundation.domain.GradeTreeData;
import com.sc.study.foundation.domain.ScSubject;
import com.sc.study.foundation.domain.ScSubjectTeacher;
import com.sc.study.foundation.mapper.ScSubjectMapper;
import com.sc.study.foundation.mapper.ScSubjectTeacherMapper;
import com.sc.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.foundation.mapper.ScGradeMapper;
import com.sc.study.foundation.domain.ScGrade;
import com.sc.study.foundation.service.IScGradeService;

/**
 * 年级Service业务层处理
 *
 * @author sc
 * @date 2022-02-27
 */
@Service
public class ScGradeServiceImpl implements IScGradeService {
    @Autowired
    private ScGradeMapper scGradeMapper;
    @Autowired
    private ScClazzMapper scClazzMapper;
    @Autowired
    private ScSubjectMapper scSubjectMapper;
    @Autowired
    private ScSubjectTeacherMapper scSubjectTeacherMapper;
    @Autowired
    private ScClazzSubjectMapper scClazzSubjectMapper;
    @Autowired
    private ScClazzSubjectTeacherMapper scClazzSubjectTeacherMapper;

    @Autowired
    ISysConfigService iSysConfigService;

    /***
     * 根据老师查询年级包含班级
     * @return
     */
    @Override
    public List<ScGrade> selectScGradeListIncludeClazzList() {
        //拿到启用状态configValue
        Long disabledState = Long.valueOf(iSysConfigService.selectConfigByKey("disabled_state"));

        //查询所有年级
        List<ScGrade> gradeList = scGradeMapper.selectScGradeList(null);
        //设置年级对应的不为禁用的班级
        gradeList.forEach(item -> {
            ScClazz scClazz = new ScClazz();
            scClazz.setGradeid(item.getId());
            scClazz.setEnbState(disabledState);
            List<ScClazz> scClazzList = scClazzMapper.selectScClazzList(scClazz);
            item.setScClazzList(scClazzList);
        });
        return gradeList;

    }

    /**
     * 查询年级
     *
     * @param id 年级主键
     * @return 年级
     */
    @Override
    public ScGrade selectScGradeById(Long id) {
        return scGradeMapper.selectScGradeById(id);
    }

    /**
     * 查询年级列表包括科目
     *
     * @param scGrade 年级
     * @return 年级
     */
    @Override
    public List<ScGrade> selectScGradeList(ScGrade scGrade) {
        List<ScGrade> scGradeList = scGradeMapper.selectScGradeList(scGrade);
        ScSubject scSubject = new ScSubject();

        scGradeList.forEach(item -> {
                scSubject.setGradeid(item.getId());
                item.setScSubjectList(scSubjectMapper.selectScSubjectList(scSubject));
            });
        return scGradeList;
    }


    /**
     * 设计年级包含科目
     */
    private void SettingGradeIncludeSubject(List<ScGrade> scGradeList, List<ScSubject> scSubjectList) {
        scGradeList.forEach(item->{
            List<ScSubject> subjectList = new ArrayList<>();
            scSubjectList.forEach(scSubject -> {
                if(scSubject.getGradeid().equals(item.getId())){
                    subjectList.add(scSubject);
                }
            });
            item.setScSubjectList(subjectList);
        });
    }

    /**
     * 新增年级
     *
     * @param scGrade 年级
     * @return 结果
     */
    @Override
    public int insertScGrade(ScGrade scGrade) {
        return scGradeMapper.insertScGrade(scGrade);
    }

    /**
     * 修改年级
     *
     * @param scGrade 年级
     * @return 结果
     */
    @Override
    public int updateScGrade(ScGrade scGrade) {
        return scGradeMapper.updateScGrade(scGrade);
    }

    /**
     * 批量删除年级
     *
     * @param ids 需要删除的年级主键
     * @return 结果
     */
    @Override
    public int deleteScGradeByIds(Long[] ids) {
        return scGradeMapper.deleteScGradeByIds(ids);
    }

    /**
     * 删除年级信息
     *
     * @param id 年级主键
     * @return 结果
     */
    @Override
    public int deleteScGradeById(Long id) {
        return scGradeMapper.deleteScGradeById(id);
    }

    @Override
    public List<ScGrade> selectScGradeListTree(GradeTreeData gradeTreeData) {
        List<ScGrade> scGradeList = scGradeMapper.selectScGradeList(null);
        // 如果传了userId就根据userId找到年级对应的课程
        ScSubjectTeacher scSubjectTeacher = new ScSubjectTeacher();

        Optional.ofNullable(gradeTreeData.getUserId()).ifPresent(id -> {
            scSubjectTeacher.setUserid(id);
            // 根据用户id查询到该用户能教的课程
            List<ScSubjectTeacher> scSubjectTeacherList = scSubjectTeacherMapper.selectScSubjectTeacherList(scSubjectTeacher);

            List<ScSubject> scSubjectList = new ArrayList<>();
            // 根据该用户能教的课程找到课程
            scSubjectTeacherList.forEach(subjectTeacher -> {
                ScSubject scSubject1 = scSubjectMapper.selectScSubjectBySubid(subjectTeacher.getSubid());
                scSubjectList.add(scSubject1);
            });

            // 循环年级
            SettingGradeIncludeSubject(scGradeList, scSubjectList);
        });

        // 如果传了clazzId就根据clazzId找到年级对应的班级课程
        ScClazzSubject scClazzSubject=new ScClazzSubject();

        Optional.ofNullable(gradeTreeData.getClazzId()).ifPresent(id->{
            scClazzSubject.setClazzid(id);
            // 根据班级id查到该班的班级课程
            List<ScClazzSubject> scClazzSubjectList = scClazzSubjectMapper.selectScClazzSubjectList(scClazzSubject);
            // 根据班级课程找对应的课程
            List<ScSubject> scSubjectList = new ArrayList<>();
            scClazzSubjectList.forEach(item->{
                scSubjectList.add(scSubjectMapper.selectScSubjectBySubid(item.getSubid()));
            });
            // 循环年级
            SettingGradeIncludeSubject(scGradeList, scSubjectList);
        });


        // 没传userId就不根据
        if(gradeTreeData.getUserId()==null&&gradeTreeData.getClazzId()==null){
            ScSubject scSubject = new ScSubject();
            scGradeList.forEach(item -> {
                scSubject.setGradeid(item.getId());
                item.setScSubjectList(scSubjectMapper.selectScSubjectList(scSubject));
            });
        }
        return scGradeList;
    }
}
